package com.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.dto.MerchantOrders;
import com.community.pojo.Consumer;
import com.community.pojo.Merchant;
import com.community.service.IConsumerService;
import com.community.service.IMerchantService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.exceptions.PersistenceException;
import com.community.utils.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@Controller
@RequestMapping("/community/merchant")
public class MerchantController {
    @Autowired
    private IMerchantService iMerchantService;

    @Autowired
    private IConsumerService consumerService;

    @RequestMapping("/checkTag/{id}")
    @ResponseBody
    public Boolean checkTag(@PathVariable Integer id) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", id);
        queryWrapper.eq("check_tag", "S001-1");

        // 执行查询，判断是否存在符合条件的记录
        // 这里假设使用了getOne方法，根据实险情况替换为合适的查询方法
        Merchant entity = iMerchantService.getOne(queryWrapper);
        // 如果查询结果不为null，返回true，否则返回false
        return entity != null;
    }

    @RequestMapping("/checkApproval/{id}")
    @ResponseBody
    public Boolean checkApproval(@PathVariable Integer id) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", id);
        queryWrapper.eq("check_tag", "S001-3");

        // 执行查询，判断是否存在符合条件的记录
        // 这里假设使用了getOne方法，根据实险情况替换为合适的查询方法
        Merchant entity = iMerchantService.getOne(queryWrapper);
        // 如果查询结果不为null，返回true，否则返回false
        return entity != null;
    }

    /**
     * 增加商户
     *
     * @param merchant
     */
    @RequestMapping("/handleSave")
    @ResponseBody
    public boolean handleSave(@RequestBody Merchant merchant) {
       return iMerchantService.save(merchant);
    }

    /**
     * 图片上传
     *
     * @param photo
     * @return
     */
    @PostMapping("/upload")
    public String upload(MultipartFile photo) {
        System.out.print(photo);
        if (photo == null) {
            return "error";
        }
        File file = new File("D:\\pictures");
        if (!file.exists()) {
            file.mkdirs();
        }
        String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
        String filename = UUID.randomUUID() + "_" + suffix;
        //写文件
        try {
            photo.transferTo(new File(file, filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = "http://localhost:10001/" + filename;
        return result;
    }
    /**
     * Consumer
     */
    /**
     * 商户查询所有
     *
     * @param pageNo
     * @param pageSize
     * @param merchant
     * @return
     */
    @RequestMapping("/queryAll/{pageNo}/{pageSize}")
    @ResponseBody
    public PageInfo<Merchant> page(@PathVariable int pageNo,
                                   @PathVariable int pageSize,
                                   @RequestBody Merchant merchant) {
        PageInfo<Merchant> pageInfo = this.iMerchantService.getMerchantList(pageNo, pageSize,
                merchant);
        System.out.println("pageInfo = " + pageInfo);
        return pageInfo;
    }

    /**
     * Fly:根据 商户id进行修改自己的信息
     *
     * @param merchant
     */
    @RequestMapping("/updateMerchantById")
    public void updateMerchantById(@RequestBody Merchant merchant) {
        // 创建QueryWrapper并根据商户ID设置查询条件
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", merchant.getMerchantId());

        // 使用update方法更新记录
        iMerchantService.update(merchant, queryWrapper);
    }

    /**
     * 获取id
     *
     * @param
     * @return
     */
    @RequestMapping("/getById/{merchantId}")
    @ResponseBody
    public Merchant getById(int merchantId) {
        Merchant merchant = iMerchantService.getById(merchantId);
        return merchant;
    }


    /**
     * 修改
     *
     * @param merchant
     * @return
     */

    @RequestMapping("/updById")
    @ResponseBody
    public String updById(@RequestBody Merchant merchant) {
        boolean flag = iMerchantService.updateById(merchant);
        if (flag) {
            return "ok";
        }
        return "error";
    }

    /**
     * 删除
     *
     * @param merchant
     * @return
     */
    @RequestMapping("delByMerchantId")
    @ResponseBody
    public String delBackUser(@RequestBody Merchant merchant) {
        iMerchantService.delByMerchantId(merchant);
        return "删除成功！";
    }

    /**
     * 条件查询
     *
     * @param merchant
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/merchantPage/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<Merchant> merchantFuHePage(
            @RequestBody Merchant merchant,
            @PathVariable int pageNo,
            @PathVariable int pageSize) {
        PageUtils<Merchant> page = this.iMerchantService.merchantFuHePage(merchant, pageNo, pageSize);
        return page;
    }

    /**
     * fly :根据用户id查询商户
     * merchant
     *
     * @param id
     * @return
     */
    @RequestMapping("/getMerchantByID/{id}")
    @ResponseBody
    public Object getMerchantByID(@PathVariable Integer id) {
        return iMerchantService.getOne(new QueryWrapper<Merchant>().eq("consumer_id", id));
    }

    @RequestMapping("/merchantGetId/{id}")
    @ResponseBody
    public Merchant designProcedureGetId(@PathVariable int id) {
        // 封装查询条件
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id", id);
        // 根据consumer_id查询Merchant
        Merchant merchant = iMerchantService.getOne(queryWrapper);
        merchant.setCheckTag("S001-3");
        this.updById(merchant);


        QueryWrapper<Consumer> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("consumer_id", merchant.getConsumerId());
        return merchant;
    }

    /**
     * 2.查询用户
     *
     * @param name 复核成功
     * @param
     * @return
     */
    @RequestMapping("/getConsumerByLoginName")
    @ResponseBody
    public Consumer getConsumerByLoginName(@RequestBody String name) {
        // 可能去除可能存在的等号字符
        name = name.replace("=", "");
        //根据log_name获取对象GetOne
        return consumerService.getOne(new QueryWrapper<Consumer>().eq("login_name", name));
    }


    //1.根据用户id，查询商户所有的订单
    @RequestMapping("/merchantByConsumerId/{id}")
    @ResponseBody
    public List<MerchantOrders> merchantByConsumerId(@PathVariable Integer id) {
        return iMerchantService.merchantByConsumerId(id);
    }

    @RequestMapping("/merchantFuHeOk")
    @ResponseBody
    public String storageCellReviewPassed(@RequestBody Merchant merchant) {
        merchant.setCheckTag("S001-3");
        boolean flag = iMerchantService.updateById(merchant);
        if (flag) {
            return "correct";
        }
        return "error";
    }

    /**
     * 复核不成功
     *
     * @param merchant
     * @return
     */
    @RequestMapping("/merchantFuHeNoOk")
    @ResponseBody
    public String storageCellReviewPassedNo(@RequestBody Merchant merchant) {
        merchant.setCheckTag("S001-2");
        boolean flag = iMerchantService.updateById(merchant);
        if (flag) {
            return "correct";
        }
        return "error";
    }

    /**
     * 添加成为商户的信息拿取
     */
   /* @RequestMapping("/add")
    @ResponseBody
    public SysMerchantDto getAddressAndInfo(@RequestBody SysMerchantDto dto) {
      return:;
    }*/
}
